﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common.ViewModel/ServiceResult 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       ServiceResult
* 创建时间：  2025/5/21 18:32:51
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Common
{
    /// <summary>
    /// 服务返回信息
    /// </summary>
    public class ServiceResultBase : IServiceResultBase
    {
        /// <summary>
        /// 成功/失败
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string? Msg { get; set; }
        /// <summary>
        /// 错误码
        /// </summary>
        public string? Code { get; set; }
    }
    /// <summary>
    /// 服务返回信息带数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ServiceResult<T> : IServiceResult<T>
    {
        /// <summary>
        /// 成功/失败
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string? Msg { get; set; }
        /// <summary>
        /// 错误码
        /// </summary>
        public string? Code { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        public ServiceResult<T> Ok(T data, string? msg = null)
        {
            Success = true;
            Data = data;
            Msg = msg;

            return this;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public ServiceResult<T> Fail(string? msg = null, T data = default)
        {
            Success = false;
            Msg = msg;
            Data = data;

            return this;
        }
    }

    /// <summary>
    /// 静态结果输出
    /// </summary>
    public static partial class ServiceResult
    {
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static IServiceResult<T> Ok<T>(T data = default(T), string msg = null)
        {
            return new ServiceResult<T>().Ok(data, msg);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static IServiceResult<string> Ok()
        {
            return Ok<string>();
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static IServiceResult<T> Fail<T>(string msg = null, T data = default)
        {
            return new ServiceResult<T>().Fail(msg, data);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static IServiceResult<string> Fail(string msg = null)
        {
            return new ServiceResult<string>().Fail(msg);
        }

        /// <summary>
        /// 系统异常
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="code">编码</param>
        /// <param name="statusCode">状态编码</param>
        /// <returns></returns>
        public static SSPivotException Exception(string msg = null, string code = null, int statusCode = (int)HttpStatusCode.OK)
        {
            return new SSPivotException(msg, code, statusCode);
        }

        /// <summary>
        /// 根据布尔值返回结果
        /// </summary>
        /// <param name="success"></param>
        /// <returns></returns>
        public static IServiceResult<T> Result<T>(bool success)
        {
            return success ? Ok<T>() : Fail<T>();
        }

        /// <summary>
        /// 根据布尔值返回结果
        /// </summary>
        /// <param name="success"></param>
        /// <returns></returns>
        public static IServiceResult<string> Result(bool success)
        {
            return success ? Ok() : Fail();
        }
    }
    /// <summary>
    /// 服务返回信息接口
    /// </summary>
    public interface IServiceResultBase
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        bool Success { get; }

        /// <summary>
        /// 消息
        /// </summary>
        string Msg { get; }

        /// <summary>
        /// 编码
        /// </summary>
        string Code { get; set; }
    }
    /// <summary>
    /// 服务返回信息带数据接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IServiceResult<T> : IServiceResultBase
    {
        /// <summary>
        /// 返回数据
        /// </summary>
        T Data { get; }
    }
}
